﻿Namespace SystemCommands

    Module mSystemCommands

        Public Sub SystemCommand(ByVal cmd As String, ByVal param As String, ByRef Conn As Connection)

            Select Case LCase(cmd)

                Case "make" '@make GM Command

                    If Conn.IsGM Then

                        Dim id As Int64 ' 64bit Integer to hold ITEM_ID for new item
                        'create the requested item
                        id = CreateNewItem(param, Conn.CharacterID)
                        'put the new item in the GM's bag

                        If id > 0 Then LoadNewItem(Conn, id)

                        'write line to console to the effect that this GM has excecuted the command
                        Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @make command [" & param & "]")

                    Else 'command was not sent by a GM

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @make command [" & param & "] " & Constants.SysCmdAttention)

                    End If

                Case "resme" '@resme GM Command - Ressurects the GM

                    If Conn.IsGM Then
                        'set HP to default res level
                        Conn.VitalPoints.HP = CInt(Conn.VitalPoints.MAXHP / Constants.Factor_ResurectedHP)
                        'set MP to default res level
                        Conn.VitalPoints.MP = CInt(Conn.VitalPoints.MAXMP / Constants.Factor_ResurectedMP)
                        'set IsAlive flag so server knows GM is now alive
                        Conn.IsAlive = True

                        'send updated HP/MP points to GM
                        Dim pkt As Packet
                        pkt.PacketType = GamePacketType.PlayerHit
                        pkt.PacketOpCode = PacketOpCode.Notify
                        pkt.Username = Conn.Username
                        Dim struct As Vitals = Conn.VitalPoints
                        Socket.SockServer.SendData(Protocol.MakePacket(pkt, struct, Conn.Crypto), Conn.Index)

                        'send updated HP/MP points to other players nearby
                        Dim pkt2 As New Packet
                        pkt2.PacketType = GamePacketType.CharacterHit
                        pkt2.PacketOpCode = PacketOpCode.Notify
                        Dim struct2 As CharacterHitInfo
                        struct2.CharacterID = Conn.CharacterID
                        struct2.VitalPoints = Conn.VitalPoints
                        SendToLocalPlayers(Conn, pkt2, struct2, True)

                        'write line to console to the effect that this GM has excecuted the command
                        Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @resme command")

                    Else 'command was not sent by a GM

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @resme command " & Constants.SysCmdAttention)

                    End If

                Case "teleport"

                    If Conn.IsGM Then

                        Try

                            'get the map file name that the GM wants to teleport to
                            Dim tmap As String = Strings.Left(param, InStr(param, ">") - 1)
                            'remove the map name and ">" character from the parameter string (param)
                            param = Strings.Right(param, param.Length - tmap.Length - 1)
                            'get the desired x coordinate
                            Dim x As Integer = Strings.Left(param, InStr(param, ":") - 1)
                            'get the desired y coordinate
                            Dim y As Integer = Strings.Right(param, param.Length - InStr(param, ":"))
                            'create PositionInfo structure (pi) and fill in the data gained from parameter string (param)
                            Dim pi As PositionInfo
                            pi.INT2_X = x
                            pi.INT2_Y = y
                            pi.STR_MAP = tmap

                            'teleport the GM
                            TeleportPlayer(Conn, pi)

                            'write line to console to the effect that this GM has excecuted the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @teleport command [Map:" & tmap & " X: " & x & " Y: " & y & "]")

                        Catch

                            'write line to console to the effect that this GM has tied to excecute the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an errornous @teleport command [" & param & "]")

                        End Try

                    Else 'command was not sent by a GM

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @teleport command [" & param & "] " & Constants.SysCmdAttention)

                    End If

                Case "godmode"

                    If Conn.IsGM Then

                        Conn.GodMode = Not Conn.GodMode

                        'write line to console to the effect that this GM has excecuted the command
                        Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @godmode command")

                    Else

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @godmode command [" & param & "] " & Constants.SysCmdAttention)

                    End If


                    'FORMAT @spawn 'NAME'@#XCOORD:#YCOORD
                Case "spawn"

                    If Conn.IsGM Then

                        Try

                            'get the mob name the gm wants to spawn
                            Dim name As String = Strings.Left(param, InStr(param, "@") - 1)
                            'get mob id from the DB
                            Dim ssql As String = "SELECT ID_MON FROM TBL_MON WHERE STR_NAME='" & name & "';"
                            Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(ssql)

                            'check for invalid mob name
                            If row Is Nothing Then
                                Packets.SendSystemMessage(Conn, "Invalid mob name!")
                            Else
                                If row.Count = 0 Then
                                    Packets.SendSystemMessage(Conn, "Invalid mob name!")
                                Else

                                    'remove the mob name and "@" character from the parameter string (param)
                                    param = Strings.Right(param, param.Length - name.Length - 1)
                                    'get the desired x coordinate
                                    Dim x As Integer = Strings.Left(param, InStr(param, ":") - 1)
                                    'get the desired y coordinate
                                    Dim y As Integer = Strings.Right(param, param.Length - InStr(param, ":"))
                                    
                                    'get map the gm is on
                                    Dim tmp As MirMap = mMaps.GetMapFromConn(Conn)
                                    'spawn the mob
                                    tmp.Mobs.SpawnMob(row("ID_MON"), x, y)

                                    'write line to console to the effect that this GM has excecuted the command
                                    Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @spawn command [Map:" & tmp.name & " X: " & x & " Y: " & y & "]")

                                End If
                            End If

                        Catch

                            'write line to console to the effect that this GM has tied to excecute the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an errornous @spawn command [" & param & "]")

                        End Try

                    Else 'command was not sent by a GM

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @spawn command [" & param & "] " & Constants.SysCmdAttention)

                    End If


                Case "addspawn"

                    If Conn.IsGM Then

                        Try

                            'get the mob name the gm wants to spawn
                            Dim pName As String = Strings.Left(param, InStr(param, ">") - 1)
                            'get mob id from the DB
                            Dim ssql As String = "SELECT ID_MON FROM TBL_MON WHERE STR_NAME='" & pName & "';"
                            Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(ssql)

                            'check for invalid mob name
                            If row Is Nothing Then
                                Packets.SendSystemMessage(Conn, "Invalid mob name!")
                            Else
                                If row.Count = 0 Then
                                    Packets.SendSystemMessage(Conn, "Invalid mob name!")
                                Else

                                    'remove the mob name
                                    param = Strings.Right(param, param.Length - pName.Length - 1)
                                    'get the desired count 
                                    Dim pCount As Integer = Strings.Left(param, InStr(param, "@") - 1)
                                    'trim
                                    param = Strings.Right(param, param.Length - CStr(pCount).Length - 1)
                                    'get the desired x coordinate
                                    Dim pRangeX As Integer = Strings.Right(param, param.Length - InStr(param, ":"))
                                    'get the desired y coord
                                    Dim pRangeY As Integer = Strings.Right(param, param.Length - CStr(pRangeX).Length - 1)


                                    'get map the gm is on
                                    Dim tmp As MirMap = mMaps.GetMapFromConn(Conn)

                                    Dim cols() As String = {"STR_MAPNAME", "ID_MON", "INT2_X", "INT2_Y", "INT2_RANGEX", "INT2_RANGEY", "INT2_COUNT"}
                                    Dim vals() As String = {tmp.file, row("ID_MON"), Conn.PositionInfo.INT2_X, Conn.PositionInfo.INT2_Y, pRangeX, pRangeY, pCount}
                                    DB.GameDB.NewRecord("TBL_MONSPAWN", cols, vals)

                                    tmp.FloorItems.AddToken(Conn, 1773, "Spawn: " & pName & vbCrLf & "Count: " & pCount)

                                    'write line to console to the effect that this GM has excecuted the command
                                    Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @addspawn command [Map:" & tmp.name & " Mob:" & pName & "']")

                                    Packets.SendSystemMessage(Conn, "@addspawn exectuted!")

                                End If
                            End If

                        Catch

                            'write line to console to the effect that this GM has tied to excecute the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an errornous @spawn command [" & param & "]")

                            Packets.SendSystemMessage(Conn, "@addspawn [$mobname]>[#count]@[#rangex]:[#rangey]")

                        End Try

                    Else 'command was not sent by a GM

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @addspawn command [" & param & "] " & Constants.SysCmdAttention)

                    End If

                Case "learnskill"

                    If Conn.IsGM Then

                        Try


                            Dim spellcode As Skills = System.Enum.Parse(GetType(Skills), param)

                            Dim ssql As String = "SELECT * FROM TBL_SKILLBOOK WHERE ID_CHARACTER=" & Conn.CharacterID & " AND BYTE_SKILL=" & spellcode & ";"
                            Dim row As Collection = DB.GameDB.ExecuteQueryFirstRecord(ssql)

                            If row.Count = 0 Then

                                Dim cols() As String = {"ID_CHARACTER", "BYTE_SKILL", "INT8_EXP", "BYTE_LEVEL"}
                                Dim vals() As String = {Conn.CharacterID, spellcode, 0, 0}
                                DB.GameDB.NewRecord("TBL_SKILLBOOK", cols, vals)

                                Packets.SendSystemMessage(Conn, "You learnt the skill " & spellcode.ToString & "!")

                                LoadPlayerSkills(Conn)

                            Else

                                Packets.SendSystemMessage(Conn, "You already know the spell!")

                            End If

                            'write line to console to the effect that this GM has excecuted the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @learnskill command [" & param & "]")

                        Catch

                            'write line to console to the effect that this GM has tied to excecute the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an errornous @learnskill command [" & param & "]")

                        End Try

                    Else

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @learnskill command [" & param & "] " & Constants.SysCmdAttention)


                    End If

                Case "levelme"

                    If Conn.IsGM Then

                        Try

                            AddExp(Conn, Int(Conn.ExpThisLvl / Globals.ServerSettings.Rate_ExpMultiplier))

                            'write line to console to the effect that this GM has excecuted the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an @levelme command")

                        Catch

                            'write line to console to the effect that this GM has tied to excecute the command
                            Output.AddLine(Constants.SysCmdGMCmdExec & GetCharAccString(Conn) & "executed an errornous @levelme command")

                        End Try

                    Else

                        'write line to console to the effect of this player has tried to excecute a GM command
                        Output.AddLine(Constants.SysCmdAttention & GetCharAccString(Conn) & "tried an @levelme command" & Constants.SysCmdAttention)


                    End If

            End Select

        End Sub

    End Module

End Namespace